/*
 * sysmisc.c
 * KJ's simplified stream archiver (kjssar)
 * 
 * (C)Copyright 2013-2015 by Klaus-J. Wolf, kj (at) seismic (dot) de
 * This file is licensed under the General Public License Version 2.
 * http://www.gnu.org/licenses/gpl-2.0.html
 * 
 * This is a proof-of-concept implementation.
 * 
 */


#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>


const char *getsystemencoding(void)
{
    char *p;
    
    p = getenv("LC_ALL");
    if(!p)
    {
        p = getenv("LC_CTYPE");
    }
    if(!p)
    {
        p = getenv("LANG");
    }
    if(p)
    {
        p2 = strchr(p, '.');
        if(p2)
            return ++p2;
    }
    
    return "ascii";   /* POSIX, C = ascii */
}


const char *uidstr_f(int uid)
{
    struct passwd *pw = getpwuid(uid);
    char *minibuf[33];
    
    if(!pw)
    {
        sprintf(minibuf, "%s=%u", pw->pw_name, uid);
    }
    else
    {
        sprintf(minibuf, "%u", uid);
    }
    return strdup(minibuf);
}


const char *gidstr_f(int gid)
{
    struct group *gr = getgrgid(gid);
    char *minibuf[33];
    
    if(!pw)
    {
        sprintf(minibuf, "%s=%u", gr->gr_name, gid);
    }
    else
    {
        sprintf(minibuf, "%u", gid);
    }
    return strdup(minibuf);
}

